package com.nm.web.controller;

import com.nm.commons.exception.SupperException;
import com.nm.orm.pojo.LabelEntity;
import com.nm.service.LabelService;
import com.nm.service.output.LabelIsUseOutput;
import com.nm.service.output.LabelOutput;
import com.nm.web.request.LabelBackListRequest;
import com.nm.web.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(path = "/label")
public class LabelController extends BaseController {

    @Autowired
    private LabelService lbSvc;

    /**
     * 标签主view
     */
    @RequestMapping(path = "")
    public ModelAndView index() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/label/index");
        return model;
    }

    /**
     * 查询所有标签
     *
     * @return
     */
    @RequestMapping(path = "/all", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List<LabelOutput>> findAll() throws SupperException {

        List<LabelOutput> lbs = lbSvc.findAll();

        return getResult(lbs);
    }

    /**
     * 增加标签视图
     */
    @RequestMapping(path = "/add", method = RequestMethod.GET)
    public ModelAndView add() throws SupperException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/label/info");
        return modelAndView;
    }

    /**
     * 增加修改标签数据提交
     */
    @RequestMapping(path = "/datapost", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result> dataPost(
            @RequestBody LabelEntity labelEntity) throws SupperException {
        if ("".equals(labelEntity.getId())) {
            labelEntity.setId(null);
        }

        lbSvc.saveOrUpdate(labelEntity);

        return getResult(null);
    }

    /**
     * 查新更新对象 展示修改视图
     *
     * @param id ID
     * @return
     */
    @RequestMapping(path = "/update/{id}", method = RequestMethod.GET)
    public ModelAndView update(@PathVariable("id") String id)
            throws SupperException {

        LabelEntity label = lbSvc.getById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/label/info");

        modelAndView.addObject("label", new Result(label));

        return modelAndView;
    }

    /**
     * 删除数据
     *
     * @param id 标签ID
     * @return
     */
    @RequestMapping(path = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity<Result> delete(@PathVariable("id") String id)
            throws SupperException {

        lbSvc.deleteById(id);

        return getResult(null);
    }

    /**
     * 后台标签列表（包含新增、选择）
     *
     * @param id
     * @return
     */
    @RequestMapping(path = "/findAll", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result> backLabelList(@RequestBody LabelBackListRequest input) throws SupperException {

        List<LabelIsUseOutput> labels = lbSvc.findAll(input.getArticleId());

        return getResult(labels);
    }

}
